package cn.com.fuhuitong.main.mine.ui.fragment

import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import cn.com.fuhuitong.R
import cn.com.fuhuitong.app.AppConstance
import cn.com.fuhuitong.app.BaseApplication
import cn.com.fuhuitong.aroute.ARouteConstance
import cn.com.fuhuitong.entity.*
import cn.com.fuhuitong.http.HttpResponseStatus
import cn.com.fuhuitong.main.access.entity.UserInfoEntity
import cn.com.fuhuitong.main.base.ViewModeFragment
import cn.com.fuhuitong.main.mine.entity.OrderSignEntity
import cn.com.fuhuitong.main.mine.vm.MineViewModel
import com.alibaba.android.arouter.launcher.ARouter
import kotlinx.android.synthetic.main.include_mine_heads.*
import kotlinx.android.synthetic.main.include_mine_order.*
import kotlinx.android.synthetic.main.include_mine_service.*

class MineFragment : ViewModeFragment<MineViewModel>() {

    override fun viewModel(): MineViewModel = ViewModelProvider(
        requireActivity(),
        ViewModelProvider.AndroidViewModelFactory(BaseApplication.INSTANCE)
    ).get(MineViewModel::class.java)

    override val layoutResId: Int
        get() = R.layout.fragment_main_mine

    override fun initView() {



        //头像-资料编辑
        image_mine_heads.setOnClickListener {
            ARouter.getInstance().build(ARouteConstance.ACTIVITY_URL_MINE_PROFILE)
                .navigation()
        }
        //昵称-资料编辑
        text_mine_nickname.setOnClickListener {
            ARouter.getInstance().build(ARouteConstance.ACTIVITY_URL_MINE_PROFILE)
                .navigation()
        }

        //查看全部
        text_mine_order_all.setOnClickListener {
            ARouter.getInstance().build(ARouteConstance.ACTIVITY_URL_ORDER_LIST)
                .withInt(AppConstance.BUNDLE_ORDER_STATUS, 0)
                .navigation()
        }
        //待支付
        text_mine_wait_pay.setOnClickListener {
            ARouter.getInstance().build(ARouteConstance.ACTIVITY_URL_ORDER_LIST)
                .withInt(AppConstance.BUNDLE_ORDER_STATUS, 1)
                .navigation()
        }
        //待收货
        text_mine_wait_take.setOnClickListener {
            ARouter.getInstance().build(ARouteConstance.ACTIVITY_URL_ORDER_LIST)
                .withInt(AppConstance.BUNDLE_ORDER_STATUS, 2)
                .navigation()
        }
        //已完成
        text_mine_complete.setOnClickListener {
            ARouter.getInstance().build(ARouteConstance.ACTIVITY_URL_ORDER_LIST)
                .withInt(AppConstance.BUNDLE_ORDER_STATUS, 3)
                .navigation()
        }

//        //注册会员
//        text_mine_register_member.setOnClickListener {
//            ARouter.getInstance().build(ARouteConstance.ACTIVITY_URL_MEMBER)
//                .withInt(AppConstance.BUNDLE_MEMBER_ACTION, 0)
//                .navigation()
//        }
        //收益中心
        text_mine_income_center.setOnClickListener {
            ARouter.getInstance().build(ARouteConstance.ACTIVITY_URL_MINE_INCOME)
                .navigation()
        }
        //分享中心
        text_mine_share_center.setOnClickListener {
            ARouter.getInstance().build(ARouteConstance.ACTIVITY_URL_MINE_SHARE)
                .navigation()
        }
        //我的收藏
        text_mine_me_collect.setOnClickListener {
            ARouter.getInstance().build(ARouteConstance.ACTIVITY_URL_MINE_COLLECTION)
                .navigation()
        }
        //收货地址
        text_mine_take_address.setOnClickListener {
            ARouter.getInstance().build(ARouteConstance.ACTIVITY_URL_MINE_ADDRESS)
                .navigation()
        }
        //帮助中心
        text_mine_help_center.setOnClickListener {
            ARouter.getInstance().build(ARouteConstance.ACTIVITY_URL_WEB_VIEW)
                .withSerializable(
                    AppConstance.BUNDLE_WEB_VIEW,
                    MainBean(type = WebConfig.HELP_CENTER, data = null)
                )
                .navigation()
        }
        //修改密码
        text_mine_change_pwd.setOnClickListener {
            ARouter.getInstance().build(ARouteConstance.ACTIVITY_URL_ACCESS)
                .withSerializable(
                    AppConstance.BUNDLE_WEB_VIEW,
                    MainBean(type = AccessAction.RESETPWD, data = null)
                )
                .navigation()
        }
        //系统设置
        text_mine_system_setting.setOnClickListener {
            ARouter.getInstance().build(ARouteConstance.ACTIVITY_URL_MINE_SETTING)
                .navigation()
        }

        val userInfo: UserInfoEntity? =
            BaseApplication.userInfo()
        if (userInfo != null)
            fillDataUserInfo(userInfo)


    }

    override fun initViewMode() {
        viewModel.orderSignDataLiveData.observe(viewLifecycleOwner, Observer { response ->
            if (response.status != HttpResponseStatus.Status.SUCCESS)
                return@Observer
            response.response?.data?.let {
                fillDataOrderSign(it)
            }
        })

        viewModel.userInfoDataLiveData.observe(viewLifecycleOwner, Observer { response ->
            if (response.status != HttpResponseStatus.Status.SUCCESS)
                return@Observer
            response.response?.data?.let {
                fillDataUserInfo(it)
            }
        })
    }

    override fun initData() {
        viewModel.orderSign()
        viewModel.getUserInfo()
    }

    override fun onRefreshLoad() {
        viewModel.getUserInfo()
    }

    override fun onEventMainThread(eventBus: MainBean<*, *>) {
        super.onEventMainThread(eventBus)
        if (eventBus.type is OrderAction) {
            viewModel.orderSign()
        }
    }

    /**
     * 设置订单计数数据
     * */
    private fun fillDataOrderSign(entity: OrderSignEntity) {
        text_badge_wait_pay.visibility =
            if (entity.status_1 == 0) View.INVISIBLE else View.VISIBLE
        text_badge_wait_take.visibility =
            if (entity.status_2 == 0) View.INVISIBLE else View.VISIBLE
        text_badge_complete.visibility =
            if (entity.status_3 == 0) View.INVISIBLE else View.VISIBLE
        text_badge_wait_pay.text = entity.status_1.toString()
        text_badge_wait_take.text = entity.status_2.toString()
        text_badge_complete.text = entity.status_3.toString()
    }

    /**
     * 设置用户信息数据
     * */
    private fun fillDataUserInfo(entity: UserInfoEntity) {
        text_mine_nickname.text = entity.nickname
        text_mine_mobile.text = entity.group_name
        text_mine_score.text = entity.score.toString()
        if (entity.group_id == 3) {
            text_mine_income_center.visibility = View.VISIBLE
            divider_mine_income.visibility = View.VISIBLE
        } else {
            text_mine_income_center.visibility = View.GONE
            divider_mine_income.visibility = View.GONE
        }
    }

}
//滑动监听改变bar颜色
//        scroll_mine_body.setOnAnimationScrollListener(object :
//            AnimationNestedScrollView.OnAnimationScrollChangeListener {
//
//            val alphaMaxHeight: Float =
//                resources.getDimensionPixelSize(R.dimen.dp_10).toFloat()
//
//            override fun onScrollChanged(dy: Float) {
//
//                val alphaNewHeight: Float = alphaMaxHeight - dy * 0.5f
//
//                var titleAlpha: Float = (255 * alphaNewHeight / alphaMaxHeight)
//                if (titleAlpha < 0)
//                    titleAlpha = 0f
//                text_mine_title.setTextColor(
//                    Color.argb(255, titleAlpha.toInt(), titleAlpha.toInt(), titleAlpha.toInt())
//                )
//
//                var barAlpha: Float =
//                    255 * (alphaMaxHeight - alphaNewHeight) / alphaMaxHeight
//                if (barAlpha > 255)
//                    barAlpha = 255f
//                frame_mine_bar.setBackgroundColor(
//                    Color.argb(barAlpha.toInt(), 255, 255, 255)
//                )
//            }
//        })
